﻿using Inspire.Classes.Common;
using Inspire.Classes.Interfaces;
using Inspire.Entities;
using Inspire.Windows;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Inspire.Pages
{
    /// <summary>
    /// Interaction logic for CustomFieldsPatrons.xaml
    /// </summary>
    public partial class CustomFieldTypes : Page, IPage, INotifyPropertyChanged
    {
        /// <summary>
        /// A reference to the common context
        /// </summary>
        private InspireEntities _context = ContextProvider.GetContextProvider().GetContext();

        /// <summary>
        /// Back the DataGrid on this page.
        /// </summary>
        private ObservableCollection<CustomFieldType> _customFieldTypes = new ObservableCollection<CustomFieldType>();
        public ObservableCollection<CustomFieldType> CustomFieldTypeResults
        {
            get
            {
                return this._customFieldTypes;
            }
        }

        /// <summary>
        /// Default Constructor.
        /// </summary>
        public CustomFieldTypes()
        {
            // Prepare the data
            IEnumerable<CustomFieldType> resultSet = from fieldType in _context.CustomFieldTypes
                                                     orderby fieldType.Category, fieldType.Name
                                                     select fieldType;
            using (IEnumerator<CustomFieldType> resultSetEnum = resultSet.GetEnumerator())
            {
                while (resultSetEnum.MoveNext())
                {
                    // Detach the entity
                    this._context.CustomFieldTypes.Detach(resultSetEnum.Current);

                    // Add the entity to the list
                    this._customFieldTypes.Add(resultSetEnum.Current);
                }
            }

            // Initialize Components
            InitializeComponent();
        }

        /// <summary>
        /// Updates the results that are shown to the user.
        /// </summary>
        private void RefreshCustomFieldTypesList()
        {
            // Refresh the list in case the fields changed
            this._customFieldTypes.Clear();
            IEnumerable<CustomFieldType> resultSet = from fieldType in _context.CustomFieldTypes
                                                     orderby fieldType.Category, fieldType.Name
                                                     select fieldType;
            using (IEnumerator<CustomFieldType> resultSetEnum = resultSet.GetEnumerator())
            {
                while (resultSetEnum.MoveNext())
                {
                    this._customFieldTypes.Add(resultSetEnum.Current);
                }
            }
        }

        #region DataGrid Editing Actions

        /// <summary>
        /// Opens the Edit Custom Field Type dialog for the selected type.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGrid_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            IInputElement element = e.MouseDevice.DirectlyOver;
            if (element != null && element is FrameworkElement)
            {
                if (((FrameworkElement)element).Parent is DataGridCell)
                {
                    var grid = sender as DataGrid;
                    if (grid != null && grid.SelectedItem != null)
                    {
                        CustomFieldType selectedCustomFieldType = (CustomFieldType)grid.SelectedItem;
                        EditCustomFieldType editingWindow = new EditCustomFieldType(selectedCustomFieldType.ShallowCopy());
                        editingWindow.Owner = Application.Current.MainWindow;
                        editingWindow.Owner.IsEnabled = false;
                        editingWindow.ShowDialog();

                        // Refresh the Data Grid, in case a field was modified or deleted
                        this.RefreshCustomFieldTypesList();
                    }
                }
            }
        }

        /// <summary>
        /// Opens the Edit Custom Field Type dialog when the user clicks the button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditCustomFieldType_Click(object sender, RoutedEventArgs e)
        {
            if (dgCustomFieldTypes.SelectedItem != null)
            {
                // Prepare and show the Edit Field Type dialog
                CustomFieldType selectedCustomFieldType = (CustomFieldType)dgCustomFieldTypes.SelectedItem;
                EditCustomFieldType editingWindow = new EditCustomFieldType(selectedCustomFieldType.ShallowCopy());
                editingWindow.Owner = Application.Current.MainWindow;
                editingWindow.Owner.IsEnabled = false;
                editingWindow.ShowDialog();

                // Refresh the Data Grid, in case a field was modified or deleted
                this.RefreshCustomFieldTypesList();
            }
        }

        /// <summary>
        /// Opens the Add Custom Field Type dialog when the user clicks the add button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddCustomFieldType_Click(object sender, RoutedEventArgs e)
        {
            // Prepare and show the Add Field Type dialog
            AddCustomFieldType newFieldTypeWindow = new AddCustomFieldType();
            newFieldTypeWindow.Owner = Application.Current.MainWindow;
            newFieldTypeWindow.Owner.IsEnabled = false;
            newFieldTypeWindow.ShowDialog();

            // Refresh the DataGrid, in case a new field was added
            this.RefreshCustomFieldTypesList();
        }

        #endregion

        #region IPage Members

        public void Page_Displayed()
        {
            return;
        }

        #endregion

        #region INotifyPropertyChanged Members
        
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

    }
}
